package view.diaLogView;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.administrator.text.R;
import com.example.administrator.text.WelComeActvity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import data.SharedPreferencesUtils;
import entity.Update;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.NetWorkstateUtil;
import util.Url;
import util.serviceUtil.UpdataService;


/**
 * Created by Administrator on 2016/6/30.
 * 版本升级提示框
 */
public class EditionDialog extends Dialog{
    private static EditionDialog mRetunDialog;
    private Context mContext;
    private List<Update> mUpdateList;
    private WelComeActvity mWelComeActvity;
    private String mAppFile;//存下载app路径
    private TextView textView2,textView3;
    private ProgressBar mProgressBar;
    private LinearLayout mLinearLayout;

    @Override public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        super.show();
    }

    public EditionDialog(Context context, List<Update> updateList, WelComeActvity welComeActvity) {
        super(context, R.style.upoad_dialog);
        this.mContext = context;
        this.mUpdateList = updateList;
        this.mWelComeActvity = welComeActvity;
        mRetunDialog = this;
    }

    public static EditionDialog getInstance(){
        return mRetunDialog;
    }

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.width = mContext.getResources().getDisplayMetrics().widthPixels;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.edition_dialog, null);
        TextView textView1 = (TextView)mView.findViewById(R.id.text_upoad_no);   //取消
        textView2 = (TextView)mView.findViewById(R.id.text_upoad_yes);          //确定
        textView3 = (TextView)mView.findViewById(R.id.textview_editon);        //提示信息
        mLinearLayout = (LinearLayout)mView.findViewById(R.id.linear_edition);
        mProgressBar = (ProgressBar)mView.findViewById(R.id.seekBar_edition); //
        if(mUpdateList.get(0).getUs().equals("1")){
          getHint("发现新的版本"+mUpdateList.get(0).getVer()+"是否升级");

        }else if(mUpdateList.get(0).getUs().equals("2")){ getHint("目前版本不可用是否升级到"+mUpdateList.get(0).getVer()); }


        textView1.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                if(mUpdateList.get(0).getUs().equals("1")){
                    mRetunDialog.dismiss();
                    if(mWelComeActvity !=null){
                        mWelComeActvity.Entry();
                    }
                }else if(mUpdateList.get(0).getUs().equals("2")){
                    mRetunDialog.dismiss();
                    mWelComeActvity.finish();
                }
            }});

        textView2.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v){
               getUpdateApk();
               showNetWorkstate();
            }
        });
        setContentView(mView, params);
    }

    /**
     * 升级版本的请求
     */
    private void getUpdateApk(){
      mAppFile = Environment.getExternalStorageDirectory()+"/"+"apkFile/newFi.apk";
        getUpdateService();
//        final File file = new File(mAppFile);
//                if(!file.exists()){
//                    getUpdateService();
//                }else {
//                    String ver = (String) SharedPreferencesUtils.getParam(mContext, HomeUtil.mHUAppVer,"");
//                    if(ver.equals(mUpdateList.get(0).getVer())){
//                            mRetunDialog.dismiss();
//                        LogUtil.eE("",".......mAppFile....."+mAppFile);
//                        LogUtil.eE("",".......mAppFile....."+mAppFile.length());
//                        FileInputStream fis = null;
//                        try {
//                            fis = new FileInputStream(file);
//                            LogUtil.eE("",".......文件大小....."+fis.available() + "B");
//                            LogUtil.eE("",".......文件名称....."+file.getName());
//                            LogUtil.eE("",".......文件的相对路径....."+file.getPath());
//                            getUpdateService();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//
//                    }else {
//                        file.delete();
//                        getUpdateService();                    }
//                }
    }

    /**
     * 下载app
     */
    private void getUpdateService(){
        textView2.setClickable(false);
        UpdataService.getTextView(textView2,textView3);
        JumpActivityUtils.getInstance().jumpUpdataService(mContext, mUpdateList.get(0).getPic(), mUpdateList.get(0).getVer(), mAppFile);
    }

    /**
     * 设置提示信息
     * @param hint
     */
    private void getHint(String hint){
        textView3.setText(hint);
    }

    /**
     * 设置进度条
     */
    public void getProgressBar(int hint){
        mProgressBar.setProgress(hint);
    }

    /**
     * 隐藏 确定和取消按钮
     */
    public void getLLayoutVisibility(){
        mLinearLayout.setVisibility(View.GONE);
    }

    /**
     * 放行 网络
     */
    private void showNetWorkstate(){
        HomeUtil.getHemeUtilNew().domainUtil(mContext, 2500, new HomeUtil.OnHomeUtilClickListener() {
            @Override
            public void onItemClick(String type) {
                if(type.equals("no")){
                    NetWorkstateUtil.getInstance().showNetworking(mContext, new NetWorkstateUtil.NetworkingOnClickListener() {
                        @Override
                        public void onItemClick(boolean state) {
                            if(state){
                                Url.getUrlNew().setNETWORKING_ATATE(1);
                            }
                        }
                    });
                }
            }
        });
    }

}
